import pygame
import random

pygame.init()

# 定义颜色
white = (255, 255, 255)
yellow = (255, 255, 102)
black = (0, 0, 0)
red = (213, 50, 80)
green = (0, 255, 0)
blue = (50, 153, 213)

# 定义显示屏幕大小
dis_width = 600
dis_height = 400

dis = pygame.display.set_mode((dis_width, dis_height))
pygame.display.set_caption('贪吃蛇小游戏')

clock = pygame.time.Clock()

snake_block = 10
snake_speed = 15
score = 0  # 初始化得分

# 使用提供的字体路径
font_path = "D:\\桌面\\DuanNingYingBiXingShu\\DuanNingYingBiXingShu-2.ttf"
try:
    font_style = pygame.font.Font(font_path, 50)  # 使用提供的字体路径
    score_font = pygame.font.Font(font_path, 35)  # 使用提供的字体路径显示得分
except IOError:
    print("字体文件加载失败，请确保字体文件路径正确")
    font_style = pygame.font.SysFont(None, 50)  # 如果字体加载失败，使用系统字体
    score_font = pygame.font.SysFont(None, 35)

def our_snake(snake_block, snake_list):
    for x in snake_list:
        pygame.draw.rect(dis, black, [x[0], x[1], snake_block, snake_block])

def message(msg, color, x, y):
    mesg = font_style.render(msg, True, color)
    dis.blit(mesg, [x, y])

def show_score(x, y):
    score_mesg = score_font.render("得分: " + str(score), True, white)
    dis.blit(score_mesg, [x, y])

def game_intro():
    dis.fill(blue)
    message("按任意键开始游戏", yellow, dis_width / 6, dis_height / 3)
    message("按D选择难度", yellow, dis_width / 6, dis_height / 3 + 50)
    pygame.display.update()

    while True:
        for event in pygame.event.get():
            if event.type == pygame.KEYDOWN:
                if event.key == pygame.K_d:  # 确保这里是正确的按键检测
                    return difficulty_selection()
                else:
                    return gameLoop()  # 如果不是按键D，直接开始游戏

def difficulty_selection():
    global snake_speed  # 声明全局变量
    dis.fill(blue)
    message("选择难度", yellow, dis_width / 6, dis_height / 3)
    message("1 - 简单", white, dis_width / 6, dis_height / 2 - 50)
    message("2 - 中等", white, dis_width / 6, dis_height / 2 + 20)
    message("3 - 困难", white, dis_width / 6, dis_height / 2 + 90)
    pygame.display.update()

    while True:
        for event in pygame.event.get():
            if event.type == pygame.KEYDOWN:
                if event.key == pygame.K_1:
                    snake_speed = 15
                    return gameLoop()
                elif event.key == pygame.K_2:
                    snake_speed = 20
                    return gameLoop()
                elif event.key == pygame.K_3:
                    snake_speed = 25
                    return gameLoop()

def gameLoop():
    global score  # 使用全局得分变量
    game_over = False
    game_close = False

    x1 = dis_width / 2
    y1 = dis_height / 2

    x1_change = 0
    y1_change = 0

    snake_list = []
    Length_of_snake = 1

    foodx = round(random.randrange(0, dis_width - snake_block) / 10.0) * 10.0
    foody = round(random.randrange(0, dis_height - snake_block) / 10.0) * 10.0

    while not game_over:
        while game_close:
            dis.fill(blue)
            message("游戏结束！按Q退出或C重新开始", red, dis_width / 6, dis_height / 3)
            show_score(dis_width / 6, dis_height / 3 + 100)  # 显示得分
            pygame.display.update()

            for event in pygame.event.get():
                if event.type == pygame.KEYDOWN:
                    if event.key == pygame.K_q:
                        game_over = True
                        game_close = False
                    if event.key == pygame.K_c:
                        return gameLoop()

        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                game_over = True
            if event.type == pygame.KEYDOWN:
                if event.key == pygame.K_LEFT:
                    x1_change = -snake_block
                    y1_change = 0
                elif event.key == pygame.K_RIGHT:
                    x1_change = snake_block
                    y1_change = 0
                elif event.key == pygame.K_UP:
                    y1_change = -snake_block
                    x1_change = 0
                elif event.key == pygame.K_DOWN:
                    y1_change = snake_block
                    x1_change = 0

        if x1 >= dis_width or x1 < 0 or y1 >= dis_height or y1 < 0:
            game_close = True
        x1 += x1_change
        y1 += y1_change
        dis.fill(blue)
        pygame.draw.rect(dis, green, [foodx, foody, snake_block, snake_block])
        snake_head = []
        snake_head.append(x1)
        snake_head.append(y1)
        snake_list.append(snake_head)
        if len(snake_list) > Length_of_snake:
            del snake_list[0]

        for x in snake_list[:-1]:
            if x == snake_head:
                game_close = True

        our_snake(snake_block, snake_list)
        show_score(10, 10)  # 显示得分
        pygame.display.update()

        if x1 == foodx and y1 == foody:
            score += 10  # 增加得分
            foodx = round(random.randrange(0, dis_width - snake_block) / 10.0) * 10.0
            foody = round(random.randrange(0, dis_height - snake_block) / 10.0) * 10.0
            Length_of_snake += 1

        clock.tick(snake_speed)

    pygame.quit()
    quit()

# 启动游戏
game_intro()